package com.cac.demo.newCode.materialStorage.service.impl;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.cac.demo.newCode.materialStorage.entity.MsSslShelves;
import com.cac.demo.newCode.materialStorage.mapper.MsSslShelvesMapper;
import com.cac.demo.newCode.materialStorage.service.IMsSslShelvesService;
import com.cac.demo.util.adapter.ResponseMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 立体库货位信息表 服务实现类
 * </p>
 *
 * @author xz
 * @since 2024-08-27
 */
@Service
public class MsSslShelvesServiceImpl extends ServiceImpl<MsSslShelvesMapper, MsSslShelves> implements IMsSslShelvesService {
    @Resource
    private MsSslShelvesMapper msSslShelvesMapper;
    @Autowired
    private Environment environment;
    /**
     * 查看缓存箱或托盘货位信息
     * @return
     */
    @Override
    public ResponseMsg<Object> getShelvesList() {
        List<MsSslShelves> msSslShelves = this.baseMapper.selectList(null);
        //筛选操作盘不为空的数据
        msSslShelves = msSslShelves.stream().filter(m -> m.getPalletCode() != null).collect(Collectors.toList());
        //筛选出托盘
        List<MsSslShelves> list1 = msSslShelves.stream().filter(m -> m.getPalletCode().contains("2120800")).collect(Collectors.toList());
        //筛选出缓存箱
        List<MsSslShelves> list2 = msSslShelves.stream().filter(m -> m.getPalletCode().contains("1120800")).collect(Collectors.toList());

        HashMap<Object,Object> hashMap = new HashMap<>();
        hashMap.put("tool", list1);
        hashMap.put("workBin", list2);
        return new ResponseMsg<>().setResponseBody(hashMap);
    }

    /**
     * 查看货位利用率
     * @return
     */
    @Override
    public ResponseMsg<Object> getShelvesUtilizationRate() {
        String shelvesUtilizationRate ="";
        List<MsSslShelves> msSslShelves = this.baseMapper.selectList(null);
        //筛掉空货位
        List<MsSslShelves> collect = msSslShelves.stream().filter(m -> m.getPalletCode() != null).collect(Collectors.toList());
        collect = collect.stream().filter(m -> m.getPalletCode() != "").collect(Collectors.toList());

        //获取货位总数
        int total = msSslShelves.size();
        //获取空货位数
        int size = collect.size();

        //获取百分比
        shelvesUtilizationRate = size*100/total+"%";

        return new ResponseMsg<>().setResponseBody(shelvesUtilizationRate);
    }
    /**
     * 获取料箱在库位的位置
     * @return
     */
    @Override
    public ResponseMsg<Object> getMsSslShelves() {
        List<HashMap> hashMap = msSslShelvesMapper.getMsSslShelves();
        return new ResponseMsg<>().setResponseBody(hashMap);
    }
    /**
     * 获取任务编号和料箱的关联信息
     * @return
     */
    @Override
    public ResponseMsg<Object> getTaskLogs() {
        List<HashMap> hashMap = msSslShelvesMapper.getTaskLogs();
        return new ResponseMsg<>().setResponseBody(hashMap);
    }

    /**
     * 获取任务编号和料箱的关联信息
     * @return
     */
    @Override
    public ResponseMsg<Object> getMaterialInfoByWorkbinCode(String boxCode) {
        List<HashMap> hashMap = msSslShelvesMapper.getMaterialInfoByWorkbinCode(boxCode);
        return new ResponseMsg<>().setResponseBody(hashMap);
    }
    /**
     * 持续推送新增的任务号和料箱的关联信息
     * @return
     */
    @Override
    public ResponseMsg<Object> sendTaskInfo(String message) {
        String MqttUrl = environment.getProperty("lesApi.sendMqttMessage");
        Map<String, String> sendData = new HashMap<>();
        sendData.put("qos","1");
        sendData.put("retained","false");
        sendData.put("topic", "sendTaskInfo");
        sendData.put("message",message);
        String result= com.cac.demo.util.HttpUtil.get(MqttUrl,sendData);
        return new ResponseMsg<>().setResponseBody(result);
    }
}
